<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Inc. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/documents
 */
class appchatController extends appRain_Base_Core
{
    public $name = 'appchat';


    /**
     * Execute before the action
     */
    public function __preDispatch()
    {
        $this->page_title = 'Chat';
		$this->set("selected","appchat");
    }


    /**
     * Render Chat Window
     */
    public function indexAction()
    {
    }


	public function postingAction()
	{
		$this->layout = 'empty';

		$fn = App::Component('appChat')->Helper('Data')->getLogLocation();
		$maxlines = 18;
		$nick_length = 9;

		/* Set this to a minimum wait time between posts (in sec) */
		$waittime_sec = 0;

		/* spam keywords */
		$spam = App::Component('appChat')->Helper('Data')->getSpamData();

		/* IP's to block */
		$blockip = App::Component('appChat')->Helper('Data')->getSpamData('ip');

		/* spam, if message IS exactly that string */
		$espam[] = "ajax";

		/* Get Message & Nick from the Request and Escape them */
		$msg = $_REQUEST["m"];
		$msg = htmlspecialchars(stripslashes($msg));

		$n = $_REQUEST["n"];
		$n = htmlspecialchars(stripslashes($n));

		if (strlen($n) >= $nick_length) {
			$n = substr($n, 0, $nick_length);
		} else {
			for ($i=strlen($n); $i<$nick_length; $i++) $n .= "&nbsp;";
		}

		if ($waittime_sec > 0) {
			$lastvisit = $_COOKIE["lachatlv"];
			setcookie("lachatlv", time());

			if ($lastvisit != "") {
				$diff = time() - $lastvisit;
				if ($diff < 5) { die();	}
			}
		}

		if ($msg != "")  {
			if (strlen($msg) < 2) { die(); }
			if (strlen($msg) > 3) {
				/* Smilies are ok */
				if (strtoupper($msg) == $msg) { die(); }
			}
			if (strlen($msg) > 150) { die(); }
			if (strlen($msg) > 15) {
				if (substr_count($msg, substr($msg, 6, 8)) > 1) { die(); }
			}

			foreach ($blockip as $a) {
				if ($_SERVER["REMOTE_ADDR"] == $a)
				{
					$msg = $this->__('Access denied. Please Contact to administrator');
					//die();
				}
			}

			$mystring = strtoupper($msg);
			foreach ($spam as $a) {
				 if (strpos($mystring, strtoupper($a)) === false) {
					/* Everything Ok Here */
				 } else {
					die();
				 }
			}

			foreach ($espam as $a) {
				if (strtoupper($msg) == strtoupper($a)) { die(); }
			}

			$handle = fopen ($fn, 'r');
			$chattext = fread($handle, filesize($fn)); fclose($handle);

			$arr1 = explode("\n", $chattext);

			if (count($arr1) > $maxlines) {
				/* Pruning */
				$arr1 = array_reverse($arr1);
				for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
				$arr2 = array_reverse($arr2);
			} else {
				$arr2 = $arr1;
			}

			$chattext = implode("\n", $arr2);

			if (substr_count($chattext, $msg) > 2) { die(); }

			$out = $chattext . $n . "&nbsp;| " . $msg . "<br>\n";
			$out = str_replace("\'", "'", $out);
			$out = str_replace("\\\"", "\"", $out);
			$handle = fopen ($fn, 'w'); fwrite ($handle, $out); fclose($handle);
		}
	}

	public function readAction()
	{
		$this->layout = 'empty';
		$fn = App::Component('appChat')->Helper('Data')->getLogLocation();//$this->rootDir('/component/appchat/log/chat.txt');
		$handle = fopen ($fn, 'r');
		$chattext = fread($handle, filesize($fn)); fclose($handle);
		echo $chattext;
	}

	public function adminAction($action="index")
	{
		$this->setadmintab('appchat');
		$this->set('action',$action);
	}
}
